<?php
namespace modules\post\form\validator;

use idea\validator\Validator;
use helper\Util;

/**
 * UniqueCategoriesName class file
 * 通过category_name字段，判断记录是否已存在
 * @author 宋欢 <iphper@yeah.net>
 * @version $Id: UniqueCategoriesName.php 1 2013-05-18 14:58:59Z huan.song $
 * @package modules.post.form.validator
 * @since 1.0
 */
class UniqueCategoriesName extends Validator
{
	/**
	 * @var string 默认出错后的提醒消息
	 */
	protected $_message = '此类别名已被使用，请更换类别名.';

	/**
	 * (non-PHPdoc)
	 * @see idea\validator.Validator::isValid()
	 */
	public function isValid()
	{
		$total = (int) $this->countByCategoryName($this->getValue());
		return (($total <= 0) == $this->getOption());
	}

	/**
	 * 通过类别名，查询表中记录数
	 * @param string $categoryName
	 * @return integer
	 */
	public function countByCategoryName($categoryName)
	{
		return Util::getDao('Categories', 'post')->countByAttributes(array(
			'category_name' => $categoryName
		));
	}
}
